package com.manong.controller;

import com.manong.entity.Permission;
import com.manong.service.PermissionService;
import com.manong.utils.Result;
import com.manong.vo.query.PermissionQueryVo;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 李茂鑫
 * @Date 2025/1/8 11:43
 * @Descripition 前端控制器
 */
@RestController
@RequestMapping("/api/Permission")
public class PermissionController {
    @Resource
    private PermissionService permissionService;

    /**
     * 查询菜单列表
     * @param permissionQueryVo
     * @return
     */
    @GetMapping("/list")
    public Result getMenuList(PermissionQueryVo permissionQueryVo){
        List<Permission> permissionList=permissionService.findpermissionList(permissionQueryVo);
        return Result.ok(permissionList);
    }

    /**
     * 查询父级菜单信息
     * @return
     */
    @GetMapping("/parent/list")
    public Result getParentList(){
        List<Permission> permissionList=permissionService.findParentPermissionList();
        return Result.ok(permissionList);
    }

    /**
     * 根据id查询菜单信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getMenuById(@PathVariable Long id){
        return Result.ok(permissionService.getById(id));
    }

    /**
     * 添加菜单
     * @param permission
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Permission permission){
        if(permissionService.save(permission)){
            return Result.ok().message("菜单添加成功");
        }
        return Result.error().message("菜单添加失败");
    }

    /**
     * 更新菜单
     * @param permission
     * @return
     */
    @PutMapping("/update")
    @CrossOrigin
    public Result update(@RequestBody Permission permission){
        if(permissionService.updateById(permission)){
            return Result.ok().message("菜单修改成功");
        }
        return Result.error().message("菜单修改失败");
    }

    /**
     * 根据id删除菜单
     * @param id
     * @return
     */
    @PutMapping("/delete/{id}")
    @CrossOrigin
    public Result delete(@PathVariable Long id){
        if(permissionService.removeById(id)){
            return Result.ok().message("菜单删除成功");
        }
        return Result.error().message("菜单删除失败");
    }

    /**
     * 检查是否有子菜单
     * @param id
     * @return
     */

    @GetMapping("/check/{id}")
    public Result check(@PathVariable Long id){
        if(permissionService.hasChildrenOfPermission(id)){
            return Result.exist().message("该菜单存在子菜单，不能删除");
        }
        return Result.ok().message("该菜单不存在子菜单，可以删除");
    }
}
